<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                <el-breadcrumb-item>用户列表</el-breadcrumb-item>
            </el-breadcrumb>
            <!--分割线-->
            <div class="yx_line"></div>
            <el-row :gutter="10" style="margin-top:20px;">
               <el-col :span='6'>
                  <el-input v-model="nickname">
                    <template slot="prepend">昵称</template>
                  </el-input>
               </el-col>
            </el-row>
            <el-row class="yx_el_row">
                <el-col>
                    <el-button type="success" plain @click="search">筛选</el-button>
                </el-col>
            </el-row>
            <el-table class="yx_table" border :data="userlist">
                <el-table-column prop="index" label="序号" width="100">
                </el-table-column>
                <el-table-column prop="avatar_url" label="头像" width="180">
                    <template slot-scope="scope">
                          <el-avatar :size="50" :src="scope.row.avatar_url"></el-avatar>
                    </template>
                </el-table-column>
                <el-table-column prop="nickname" label="昵称" width="180">
                </el-table-column>
                <el-table-column prop="gender" label="性别" width="150">
                </el-table-column>
                <el-table-column prop="area" label="地区" width="150">
                </el-table-column>
                <el-table-column prop="phone" label="手机号" width="150">
                </el-table-column>
                <el-table-column prop="create_time" label="加入时间">
                </el-table-column>
                <!-- <el-table-column  label="操作">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="设置" placement="bottom-start">
                            <el-button type="primary" icon="el-icon-setting" @click="set_info(1,scope.row.id)" circle></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column> -->
            </el-table>
            <el-pagination class="yx_page"
                           background
                           @current-change="handle_current_change"
                           @size-change="handle_size_change"
                           :page-sizes="page_sizes"
                           :page-size="page_size"
                           layout="prev, pager, next,sizes, total"
                           :total="total_count">
            </el-pagination>
        </el-card>
        <!--<el-dialog title="用户设置" :visible="showDialog" :show-close=false width="30%">
            <el-form ref="set_form" :model="set_form" label-width="80px">
                <el-form-item label="身份">
                    <el-radio v-model="set_form.is_grider" label="0">普通用户</el-radio>
                      <el-radio v-model="set_form.is_grider" label="1">网格员</el-radio>
                </el-form-item>
                <el-form-item label="所属社区">
                    <el-select v-model="set_form.c_id" @change="handleComChange" placeholder="请选择社区">
                        <el-option
                                v-for="item in basic_data"
                                :key="item.c_id"
                                :label="item.c_name"
                                :value="item.c_id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属网格">
                    <el-select v-model="set_form.g_id" placeholder="请选择网格">
                        <el-option
                                v-for="item in allgridlist"
                                :key="item.g_id"
                                :label="item.g_name"
                                :value="item.g_id">
                        </el-option>
                     </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitSet('set_form')" :loading="loading">{{button_name}}</el-button>
                <el-button @click="set_info(0,0)">关闭</el-button>
            </div>
        </el-dialog>-->
    </div>
</template>

<script>
    export default {
        name: 'UserList',
        inject:['reload'],
        data() {
            return {
                current_page:1,
                page_size:20,
                showDialog:false,
                button_name:'立即设置',
                loading:false,
                set_form:{
                   is_grider:'0',
                   c_id:'',
                   g_id:''
                },
                current_set_id:'',
                allgridlist:[],
                nickname:'',
                is_grider:''
            }
        },
        methods: {
            getList(){
                var send_data={
                    api_token:this.$store.getters.getSessionId,
                    current_page:this.current_page,
                    page_size:this.page_size,
                    nickname:this.nickname,
                    is_grider:this.is_grider==''?-1:this.is_grider
                }
                this.$store.dispatch('loadUserList',send_data);
            },
            //跳转页
            handle_current_change(current_page){
                //当前页码数
                this.current_page=current_page;
                this.getList();
            },
            //修改条数
            handle_size_change(page_size){
                this.current_page=1;
                this.page_size=page_size;
                this.getList();
            },
            handleComChange(c_id){
                this.set_form.g_id='';
                this.getGridList(c_id);
            },
            //查询所有网格
            getGridList(c_id){
               for(let i=0;i<this.basic_data.length;i++){
                   if(c_id==this.basic_data[i]['c_id']){
                     this.allgridlist=this.basic_data[i]['children'];
                     break;
                   }
               }
            },
            search(){
               this.current_page=1;
               this.getList();
            },
            //设置
            set_info(type,id){
                if(type==1){
                    this.current_set_id=id;
                    let send_data={
                       api_token:this.$store.getters.getSessionId
                    };
                    //获取社区和网格的关系
                    this.$store.dispatch('loadBasicData',send_data);
                    this.showDialog=true;
                }else{
                    //关闭置空communitylist
                    this.$store.commit('setUserInfo',[]);
                    this.showDialog=false;
                }
            },
            submitSet(formName){
                let that = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading=true;
                        this.button_name='正在提交';
                        var send_data={
                            api_token:this.$store.getters.getSessionId,
                            is_grider:this.set_form.is_grider,
                            c_id:this.set_form.c_id,
                            g_id:this.set_form.g_id,
                            id:this.current_set_id
                        };
                        this.$store.dispatch('editUser',send_data);
                    }else{
                        return false;
                    }
                })
            }
        },
        created() {
            //加载列表
            this.getList();
        },
        computed:{
            userlist(){
                return this.$store.getters.getUserList;
            },
            total_count(){
                return this.$store.getters.getUserTotalCount;
            },
            page_sizes(){
                return this.$store.state.page_sizes;
            },
            userinfo(){
               return this.$store.getters.getUserInfo;
            },
            editUserStatus(){
               return this.$store.getters.getEditUserStatus;
            },
            basic_data(){
               return this.$store.getters.getBasicData;
            }
        },
        watch:{
            userinfo(val){
               if(val.id!=undefined){
                  this.set_form.is_grider=val.is_grider.toString();
                  this.set_form.c_id=val.c_id==0?'':val.c_id;
                  if(val.c_id>0){
                     this.getGridList(val.c_id);
                  }
                  this.set_form.g_id=val.g_id==0?'':val.g_id;
               }
            },
            editUserStatus(val){
                let that=this;
                if(val==1){
                    //按钮置为失效
                    this.$message({
                        message:'设置成功',
                        type:'success',
                        duration:1000,
                        onClose:function(e){
                            that.showDialog=false;
                            that.$store.commit('setEditUserStatus',-1);
                            that.reload();
                        }
                    });
                }else if(val==0){
                    this.$message({
                        message:'设置失败',
                        type:'error',
                        duration:2000
                    });
                    that.loading=false;
                    that.button_name='立即设置';
                    //初始化状态
                    that.$store.commit('setEditUserStatus',-1);
                }
            },
            basic_data(val){
               var send_data={
                   api_token:this.$store.getters.getSessionId,
                   id:this.current_set_id
               };
               this.$store.dispatch('loadUserInfo',send_data);
            }
        }
    }
</script>

<style>
     @import url('../../assets/css/table.css');
</style>
